package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.member.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("strategyDetail")
public class StrategyDetailController {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyService strategyService;

    @Reference
    private IStrategyThemeService strategyThemeService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private  IStrategyTagService strategyTagService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")StrategyDetailQuery qo){
        //pageInfo
        model.addAttribute("pageInfo", strategyDetailService.query(qo));
        return "strategyDetail/list";
    }

    @RequestMapping("input")
    public String input(Long id, Model model){
        //编辑
        if (id != null){
            //通过id查看查找当前的明细，里面没有明细内容
            StrategyDetail strategyDetail = strategyDetailService.get(id);
            //查询对应id的明细内容
            StrategyContent strategyContent = strategyDetailService.getContentById(id);
            strategyDetail.setStrategyContent(strategyContent);//把对应的内容存进明细中
            model.addAttribute("strategyDetail",strategyDetail);
            //标签回显
            String tags =strategyTagService.getTagsToString(id);
            model.addAttribute("tags", tags);
        }

        //查询所属攻略
        List<Strategy> strategies = strategyService.list();
        model.addAttribute("strategies",strategies);
        //查询所有的主题
        List<StrategyTheme> strategyThemes = strategyThemeService.list();
        model.addAttribute("themes",strategyThemes);
        return "strategyDetail/input";
    }

    @RequestMapping("getCatalogByStrategyId")
    @ResponseBody
    public Object getCatalogByStrategyId(Long strategyId){
        return strategyCatalogService.getCatalogByStrategyId(strategyId);
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StrategyDetail detail, String tags){
        strategyDetailService.saveOrUpdate(detail, tags);
        return AjaxResult.SUCCESS;
    }
}
